<?php
    class AppController extends Controller {
        var $components = array('Auth', 'Session', 'Cookie');
        var $helpers = array('Html', 'Ajax', 'Javascript', 'Form', 'Session');

        function beforeFilter() {
            $this->Auth->loginAction = array('controller' => 'people', 'action' => 'login');
            $this->Auth->loginRedirect = array('controller' => 'events', 'action' => 'index');
            $this->Auth->allow('register'); // We need a non logged in user to be able to register
            $this->Auth->allow('display'); // Access to homepage
            $this->Auth->authorize = 'controller';
            $this->Auth->loginError = 'Incorrect login info';
            $this->Auth->authError = "You are not logged in";
            $this->Auth->userModel = 'Person';
            $this->Auth->autoRedirect = false;

			$this->loadModel('Family');
			$this->loadModel('Person');
			$uid = $this->Auth->user('id');
			$isAdmin = false;

			foreach ($this->Person->getFamiliesFromPerson($uid) as $family) {
				if ($this->Family->isPersonFamilyAdmin($uid, $family['id'])) {
					$isAdmin = true;
					break;
				}
			}
	
			// make the user information available to all the views
			$this->set('currentUser_username' , $this->Auth->user('username'));
			$this->set('currentUser_first_name' , $this->Auth->user('first_name'));
			$this->set('currentUser_last_name' , $this->Auth->user('last_name'));
			$this->set('isAdmin', $isAdmin);
        }

        /**
         * Takes array of named params, compares them to mandatory and optional fields.
         * All mandatory must be present else returns false.
         * Any optional fields that aren't present will use the default value in the associative
         * array.
         *
         *
         * This function was taken from the CakePHP Bakery:
         *     http://bakery.cakephp.org/
         */
        function extractNamedParams($mandatory, $optional = array()) {
            $params = $this->params['named'];

            // return false immediately if no params are passed in
            if(empty($params)) {
                return false;
            }

            // convert to named index array
            $mandatory = array_flip($mandatory);
            // define array with all keys to be present
            $all_named_keys = array_merge($mandatory, $optional);
            // extract valid keys from passed params
            $valid = array_intersect_key($params, $all_named_keys);
            // fill valid array with any missing optional params
            $output = array_merge($optional, $valid);
            // check that all keys are present in final output
            $diff = array_diff_key($all_named_keys, $output);

            if (empty($diff)) {
                return $output;
            } else {
                return false;
            }
        }

        /**
         * Our default action if isAuthorized ever gets called here should be true. This
         * prevents us from having a bunch of (if action) return trues everywhere in our
         * controllers.
         */
        function isAuthorized() {
            return true;
        }
    }
?>
